<%@ page language="java"  pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script type="text/javascript" charset="utf-8">
$.formatString = function(str) {
	for ( var i = 0; i < arguments.length - 1; i++) {
		str = str.replace("{" + i + "}", arguments[i + 1]);
	}
	return str;
};
$.canEdit = true;
	$(function() {
		window.setTimeout(function(){
			$('#admin_qtth_searchForm input[name="tid"]').focus();
	    }, 0);
		$('#admin_qtth_datagrid').datagrid({
			url : ' ',
			title : '',
			iconCls : 'icon-save',
			pagination : true,
			pageSize : 5,
			pageList : [ 5, 10, 15, 20 ],
			fit : true,
			fitColumns : true,
			nowrap : false,
			border : false,
			idField : 'id',
			rownumbers: true,
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 100,
				hidden :true
			},{
				field : 'tid',
				title : '流水号',
				width : 100,
			}, {
				field : 'code',
				title : '商品条形码编码',
				width : 100
			}, {
				field : 'name',
				title : '商品名称',
				width : 100
			}, {
				field : 'unit',
				title : '商品单位',
				width : 100
			},{
				field : 'price',
				title : '商品单价',
				width : 100,
				sortable:true
			},{
				field : 'amount',
				title : '商品数量',
				width : 100,
				sortable:true
			},{
				field : 'vip',
				title : '会员',
				width : 100,
				hidden : true
			},{
				field : 'discount',
				title : '折扣',
				width : 100,
				hidden : true
			},{
				field : 'nprice',
				title : '金额',
				width : 100,
				sortable:true
			}, {
				field : 'action',
				title : '操作',
				width : 50,
				formatter : function(value, row, index) {
					var str = '';
					if ($.canEdit) {
						str += $.formatString('<img src="{0}" title="退货"/><a style="text-decoration:none" onclick="returnFun(\'{1}\');">退货</a>', '${pageContext.request.contextPath}/images/money.png',row.id);
					}
					return str;
				}
			} ] ],
			toolbar:[{
				text : '打印凭条',
				iconCls : 'icon-print',
				handler : function(){
					
				}
			},{
				text : '结束本次退货',
				iconCls : 'icon-no',
				handler : function(){
					$('#returnid input[name="salereturnno"]').val('');
					$('#admin_qtth_datagrid').datagrid('loadData',{total:0,rows:[]});
					window.setTimeout(function(){
						$('#admin_qtth_searchForm input[name="tid"]').focus();
					}, 0);
				}
			}]
		});
		$('#admin_qtth_searchForm').form({ 
		      url : '${pageContext.request.contextPath}/shopListAction!show.action',
		      success:function(r){
		      var obj=jQuery.parseJSON(r);
		      if(obj.rows.length>0){
		    	  var salereturnno=$('#returnid input[name="salereturnno"]').val();
			      if(salereturnno==null||salereturnno==""||salereturnno==undefined){
			      getReturnId(); 
			      }
			      for(var i=0;i<obj.rows.length;i++){
		    	    $('#admin_qtth_datagrid').datagrid('insertRow',{
				       index:i,
				       row:{
				    	   id:obj.rows[i].id,
				    	   tid:obj.rows[i].tid,
				    	   code:obj.rows[i].code,
				    	   name:obj.rows[i].name,
				    	   amount:obj.rows[i].amount,
				    	   unit:obj.rows[i].unit,
				    	   price:obj.rows[i].prices,
				    	   nprice:obj.rows[i].price,
				    	   discount:obj.rows[i].discount,
				    	   vip:obj.rows[i].vip
				    	}
				  });
			      }
		    	 /*  $('#admin_qtth_searchForm input[name="tid"]').val('');
		    	  window.setTimeout(function(){
			  			$('#admin_qtth_searchForm input[name="tid"]').focus();
			  	  }, 0); */
		    	  $('#admin_qtth_datagrid').datagrid('acceptChanges');
				  $('#admin_qtth_datagrid').datagrid('load');
				  $('#admin_qtth_datagrid').datagrid('unselectAll');
		      }else{
		    	  $.messager.show({
				    	title:'警告',
				    	msg:'没有查到相关记录!'
				    	});
		    	  $('#admin_qtth_searchForm input[name="tid"]').val('');
		    	  window.setTimeout(function(){
		  			$('#admin_qtth_searchForm input[name="tid"]').focus();
		  	      }, 0);
		      }
		}
	});
		getReturnId=function(){
			$.ajax({
				type:'POST',
				url:'${pageContext.request.contextPath}/cashierAction!getOrderId.action',
				dataType:'json',
				success :function(r){
					if(r&&r.success){
						$('#returnid').form('load',{
							salereturnno:r.message
						});
					}
				}
			});
		};
		checkout=function(){
			var rows=$('#admin_qtth_datagrid').datagrid('getSelections');
			var amount=rows[0].amount;
			$('#number').numberspinner({
			    min: 1,
			    max: amount,
			    editable: false,
			    onSpinUp : function(){
			    	refund();
			    },
			    onSpinDown : function(){
			    	refund();
			    }
			});
		};
	   refund=function(){
		   var rows = $('#admin_qtth_datagrid').datagrid('getSelections');
		   var price = rows[0].price;
		   var quantity = $('#number').numberspinner('getValue');  
		   var discount = rows[0].discount;
		   var refundment=price*quantity*discount;
		   $('#returnForm input[name="refundment"]').attr('value',refundment.toFixed(2));
		};
	   returnFun=function(){
			   returnDialog=parent.dialog({
					title : '退货',
					href : '${pageContext.request.contextPath}/layout/returngood.jsp',
					width : 300,
					height : 400,
					buttons : [ {
						text : '退货',
						iconCls :'icon-ok',
						handler : function() {
							var f = returnDialog.find('form');
							f.form('submit', {
								url : '${pageContext.request.contextPath}/saleReturnAction!save.action',
								success : function(d) {
									var json = $.parseJSON(d);
									if (json.success) {
										saleReturnItem();
									}
									parent.messagerShow({
										msg : json.msg,
										title : '提示'
									});
									returnDialog.dialog('close');
									$('#admin_qtth_datagrid').datagrid('loadData',{total:0,rows:[]});
									searchFun();
									/* $('#admin_qtth_datagrid').datagrid('unselectAll');
									window.setTimeout(function(){
										$('#admin_qtth_searchForm input[name="tid"]').focus();
									}, 0); */
								}
							});
						}
					} ,{
						text : '取消',
						iconCls :'icon-no',
						handler : function() {
						    returnDialog.dialog('close');
							window.setTimeout(function(){
								$('#admin_qtth_searchForm input[name="tid"]').focus();
						}, 0);
							$('#admin_qtth_datagrid').datagrid('acceptChanges');
							$('#admin_qtth_datagrid').datagrid('load');
							$('#admin_qtth_datagrid').datagrid('unselectAll');
						}
					}],
					onLoad : function() {
						var f = returnDialog.find('form');
						var salereturnno=$('#returnid input[name="salereturnno"]').val();
						var rows=$('#admin_qtth_datagrid').datagrid('getSelections');
						var tid=rows[0].tid;
						var name=rows[0].name;
						var price=rows[0].price;
						f.form('load', {
							tid : tid,
							salereturnno : salereturnno,
							name : name,
							price : price,
							quantity : '1'
						});
						checkout();
						refund();
					}			    
				});	
		};
		saleReturnItem=function(){ 
	    		  var tid=$('#returnForm input[name="tid"]').val();
	    		  var salereturnno=$('#returnForm input[name="salereturnno"]').val();
	    		  var name=$('#returnForm input[name="name"]').val();
	    		  var quantity=$('#returnForm input[name="quantity"]').val();
	    		  var price=$('#returnForm input[name="price"]').val();
	    		  var refundment=$('#returnForm input[name="refundment"]').val();
	    		  var rows=$('#admin_qtth_datagrid').datagrid('getSelections');
				  var vip=rows[0].vip;
	    			$.ajax({
							url:'${pageContext.request.contextPath}/saleReturnItemAction!save.action',
							data:{
								tid : tid,
								vip : vip,
								salereturnno : salereturnno,
								name : name,
								quantity : quantity,
								price : price,
								refundment : refundment
								},
							dataType:'json',
							success:function(r){
								if(r&&r.success){
									
								}else{					
							  }
							}
						});
		};
	   searchFun=function() {
		   $('#admin_qtth_searchForm').submit();
	  };
	   clearFun=function() {	
		$('#admin_qtth_datagrid').datagrid('loadData',{total:0,rows:[]});
		$('#admin_qtth_searchForm input[name="tid"]').val('');
  	    window.setTimeout(function(){
			$('#admin_qtth_searchForm input[name="tid"]').focus();
	    }, 0);
	};
});
</script>
<div id="admin_qtth_layout" class="easyui-layout" data-options="border:false,fit:true">
<div region="north" data-options="border:false,title:'过滤信息'" style="height:65px;overflow:hidden;">
<form id="admin_qtth_searchForm">
<div style="float:left;margin:5px;"><input name="tid" style="width:200px;"></input>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="searchFun();">查询</a>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="clearFun();">清空</a>
</div>
</form>
<div style="float:right;margin:5px;">
<form id="returnid"><label style="font-weight:bolder;margin:8px;">No:</label><input name="salereturnno" style="width:150px;"/>
</form></div>
</div>
<div region="center" data-options="border:false">
<table id="admin_qtth_datagrid"></table>
</div>
</div>